import Event from "sap/ui/base/Event";
import Target from "sap/ui/core/routing/Target";
import { BaseController } from "sap/ui/demo/nav/controller/BaseController";

interface NotFoundData {
  fromTarget: string;
}

/**
 * @namespace sap.ui.demo.nav.controller
 */
export default class NotFound extends BaseController {
  private _oData: NotFoundData | null = null;

  onInit(): void {
    const oTarget = this.router.getTarget("notFound") as Target;
    oTarget.attachDisplay((oEvent: Event) => {
      this._oData = oEvent.getParameter("data");
    }, this);
  }

  onNavBack(): void {
    if (this._oData && this._oData.fromTarget) {
      this.router.getTargets()?.display(this._oData.fromTarget);
      this._oData.fromTarget = "";
      return;
    }

    super.onNavBack.call(this);
  }
}
